export default {
  name: 'AddDefaultForm',
  components: {
    Upload: () => import('@/component/approval/Upload'),
    omsPurchaseTitle: () => import('@/component/purchase/purchaseTitle')
  },
  data () {
    return {
      taskEntitys: [], // 审批记录
      form: {
        projectName: '',
        projectAttributes: 1,
        projectType: 1,
        principal: '',
        principalPhone: '',
        concatPerson: '',
        concatPersonPhone: '',
        concatAddress: '',
        requestOrgan: '',
        address: '',
        annex: [
          {
            label: '设计方案',
            value: []
          },
          {
            label: '设施图表',
            value: []
          },
          {
            label: '营业执照',
            value: []
          },
          {
            label: '检测报告',
            value: []
          },
          {
            label: '身份证明',
            value: []
          },
          {
            label: '运钞车停靠位置',
            value: []
          },
          {
            label: '周边环境平面图',
            value: []
          },
          {
            label: '其他资料',
            value: []
          },
          {
            label: '中期检测',
            value: []
          },
          {
            label: '工程验收',
            value: []
          }
        ]
      }
    }
  },
  computed: {
    eDisabled () {
      return !(this.taskEntitys.length === 0 || this.taskEntitys.every(item => item.resultStatus === 2))
    },
    showMiddleData () {
      const isAf = this.formType === 'afgcys'
      return isAf && this.taskEntitys.length && this.taskEntitys.some(item => item.resultStatus === 2)
    }
  },
  created () {
    if (this.$route.path.includes('jsfasp')) {
      this.formType = 'jsfasp'
      this.backPath = `/approval/DefaultApproval/${this.formType}`
    } else if (this.$route.path.includes('afgcys')) {
      this.formType = 'afgcys'
      this.backPath = `/approval/AFApproval/${this.formType}`
    }
    if (this.$route.params.id) {
      this.getDetail()
    }
  },
  methods: {
    async getDetail () {
      const { data } = await this.$service.gzl.getDefaultForm(this.$route.params.id)
      this.taskEntitys = data.data.taskEntitys // 审批日志
      this.form.projectName = data.data.projectName
      this.form.address = data.data.address
      this.form.projectAttributes = data.data.projectAttributes
      this.form.projectType = data.data.projectType
      this.form.principal = data.data.principal
      this.form.principalPhone = data.data.principalPhone
      this.form.concatPerson = data.data.concatPerson
      this.form.concatPersonPhone = data.data.concatPersonPhone
      this.form.concatAddress = data.data.concatAddress
      this.form.requestOrgan = data.data.requestOrgan
      this.form.annex = JSON.parse(data.data.annex)
    },
    validate (form = 'form') {
      return new Promise((resolve, reject) => {
        this.$refs[form].validate(valid => {
          if (valid) return resolve(true)
          reject(new Error('fail'))
        })
      })
    },
    async onSubmit () {
      await this.validate()
      const params = {
        ...this.form,
        annex: JSON.stringify(this.form.annex),
        formType: this.formType
      }
      if (this.$route.params.id) {
        await this.$service.gzl.editDefaultForm(this.$route.params.id, params)
        this.$message.success('编辑成功')
      } else {
        await this.$service.gzl.addDefaultForm(params)
        this.$message.success('添加成功')
      }
      this.$closeCurrentGoEdit(this.backPath)
    }
  }
}
